package queue

import (
	"errors"
)

type Queue struct {
	data     []any
	capacity int32
	size     int32
}

func New(initSize int32) *Queue {
	return &Queue{
		data:     make([]any, initSize),
		capacity: initSize,
	}
}

func (q *Queue) Offer(e any) (err error) {
	if q.isFull() {
		err = errors.New("queue is full")
		return
	}
	q.data[q.size] = e
	q.size++
	return
}

func (q *Queue) Poll() (any, error) {
	if q.IsEmpty() {
		return nil, errors.New("queue is empty")
	}
	ele := q.data[q.size-1]
	q.size--
	return ele, nil
}

func (q *Queue) Peek() (any, error) {
	if q.IsEmpty() {
		return nil, errors.New("queue is empty")
	}
	return q.data[q.size-1], nil
}

func (q *Queue) isFull() bool {
	return q.size >= q.capacity
}

func (q *Queue) IsEmpty() bool {
	return q.size == 0
}
